<template>
  <div class='container'>
    <el-form label-width="100px">
      <el-form-item label="学科名称">
        <el-input v-model="searchSubjectsName" size="small"></el-input>
        <el-button @click="clearSubject" size="small">清除</el-button>
        <el-button @click="searchSubjects" size="small" type="primary">搜索</el-button>
        <el-button @click="addSubjectbutton" class="addbutton" size="small" type="success" icon="el-icon-edit">新增学科</el-button>
      </el-form-item>
    </el-form>
    <div class="el-table"><i class="el-icon-info"></i>数据一共 {{counts}} 条</div>
    <el-table :data="items" stripe style="width: 100%">
      <el-table-column type="index" label="序号" width="80"></el-table-column>
      <el-table-column prop="subjectName" label="学科名称" width="90"></el-table-column>
      <el-table-column prop="username" label="创建者" width="80"></el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="200">
        <template v-slot='slotProps'>
          <p>{{slotProps.row.addDate | formData}}</p>
        </template>
      </el-table-column>
      <el-table-column prop="isFrontDisplay" label="前台是否显示" width="70">
        <template v-slot='sProps'>
          <p>{{sProps.row.isFrontDisplay === 0 ? '是' : '否'}}</p>
        </template>
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录" width="100"></el-table-column>
      <el-table-column prop="tags" label="标签" width="50"></el-table-column>
      <el-table-column prop="totals" label="题目数量" width="80"></el-table-column>
      <el-table-column v-slot="scope" label="操作">
        <el-button @click="subjectClassify(scope.row.id, scope.row.subjectName)" type="text" size="small">学科分类</el-button>
        <el-button @click="subjecLabel(scope.row.id, scope.row.subjectName)" type="text" size="small">学科标签</el-button>
        <el-button @click="subjectAmend(scope.row.subjectName, scope.row.id)" type="text" size="small">修改</el-button>
        <el-button @click="subjectDelete(scope.row.id)" type="text" size="small">删除</el-button>
      </el-table-column>
    </el-table>
    <el-pagination @current-change="changePager" :current-page="page" background :total="counts" layout="prev, pager, next, jumper, sizes, slot, ->, total" :page-count="pages">
    </el-pagination>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>
        <el-form label-width="80px">
          <el-form-item label="学科名称">
            <el-input v-model="subjectName" size="small" class="input-width"></el-input>
          </el-form-item>
          <el-switch style="display: block" v-model="switchValue" active-color="#13ce66" inactive-color="#ff4949" inactive-text="是否显示">
          </el-switch>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubject">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, update, remove, add } from '@/api/hmmm/subjects.js'
import { parseTimeByString } from '@/filters/index.js'
export default {
  name: 'Subjects',
  data () {
    return {
      id: '',
      // 开关状态
      switchValue: 1,
      // 控制按钮状态
      dialogVisible: false,
      // 页大小
      pagesize: 10,
      // 当前页码
      page: 1,
      // 总页数
      pages: 0,
      // 总记录数
      counts: 0,
      // 列表数据
      items: [],
      // 学科名称
      subjectName: '',
      // 搜索
      searchSubjectsName: ''
    }
  },
  filters: {
    formData (time) {
      return parseTimeByString(time)
    }
  },
  methods: {
    // 学科标签
    subjecLabel (id, name) {
      this.$router.push('/subjects/tags?id=' + id + '&' + 'name=' + name)
    },
    // 学科分类
    subjectClassify (id, name) {
      this.$router.push('/subjects/directorys?id=' + id + '&' + 'name=' + name)
    },
    // 清空输入框
    clearSubject () {
      this.searchSubjectsName = ''
    },
    // 搜索学科
    async searchSubjects () {
      try {
        await list({
          subjectName: this.searchSubjectsName,
          page: this.page,
          pagesize: this.pagesize
        })
        this.subjectName = this.searchSubjectsName
        this.loadSubjectsList()
      } catch (e) {
        console.log(e)
        this.$message.error('搜索失败')
      }
    },
    // 学科删除
    subjectDelete (id) {
      this.$confirm('确定删除吗?').then(async () => {
        try {
          await remove({
            id: id
          })
          this.loadSubjectsList()
        } catch (e) {
          console.log(e)
          this.$message.error('删除失败')
        }
      })
    },
    // 修改学科 和 添加学科
    async editSubject () {
      if (this.id) {
        try {
          await update({
            id: this.id,
            subjectName: this.subjectName
          })
        } catch (e) {
          console.log(e)
          this.$message.error('学科修改失败!')
        }
      } else {
        try {
          await add({
            subjectName: this.subjectName,
            isFrontDisplay: this.switchValue
          })
        } catch (e) {
          console.log(e)
          this.$message.error('添加学科失败!')
        }
      }
      this.id = ''
      this.subjectName = ''
      this.dialogVisible = false
      this.switchValue = 1
      this.loadSubjectsList()
    },
    // 添加学科按钮
    addSubjectbutton () {
      this.dialogVisible = true
      this.subjectName = null
      this.id = null
    },
    // 触发修改选项
    subjectAmend (subjectName, id) {
      this.dialogVisible = true
      if (id) {
        this.subjectName = subjectName
        this.id = id
      }
    },
    async loadSubjectsList () {
      try {
        const ret = await list({
          subjectName: this.subjectName,
          pagesize: this.pagesize,
          page: this.page
        })
        this.items = ret.data.items
        this.counts = ret.data.counts
        this.pages = ret.data.pages
        // this.page = ret.data.page
      } catch (e) {
        console.log(e)
        this.$message.error('学科列表获取失败')
      }
    },
    changePager (page) {
      // 实现分页切换
      this.page = page
      this.loadSubjectsList()
    }
  },
  created () {
    this.loadSubjectsList()
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 10px 20px 10px 30px;
  background-color: #fff;
  .el-table {
    // height: 35px;
    line-height: 35px;
    padding: 0 10px;
    background-color: #f4f4f5;
    border-radius: 3px;
  }
}
.el-input {
  width: 20%;
  display: inline-block;
}
.el-button {
  margin-left: 10px;
  display: inline-block;
}
.addbutton {
  float: right;
}
.input-width {
  width: 80%;
}
</style>
